using System.Collections.Generic;
using System.Xml.Linq;
using Microsoft.SharePoint;
using SharePoint.DataMapper;

namespace SharePoint.DataMapper
{
	public class Caml
	{
		public static ViewExpression View()
		{
			return new ViewExpression(null);
		}

		public static QueryExpression Query(WhereExpression whereExpression, OrderByExpression orderByExpression)
		{
			return new QueryExpression(null, whereExpression, orderByExpression);
		}

		public static WhereExpression Where(LogicalExpression expression)
		{
			return new WhereExpression(null, expression);
		}

		public static OrderByExpression OrderBy(params FieldRefExpression[] fieldRefExpression)
		{
			return new OrderByExpression(null, fieldRefExpression);
		}

		public static OrderByExpression OrderBy(IEnumerable<FieldRefExpression> fieldRefExpression)
		{
			return new OrderByExpression(null, fieldRefExpression);
		}

		public static FieldRefExpression FieldRef(string fieldName, string fieldType)
		{
			return new FieldRefExpression(null, fieldName, fieldType);
		}

		public static FieldRefExpression FieldRef(string fieldName, SPFieldType fieldType)
		{
			return new FieldRefExpression(null, fieldName, fieldType.ToString());
		}
	
		public static FieldRefExpression FieldRef(SPField field)
		{
			return new FieldRefExpression(null, field.InternalName, field.TypeAsString);
		}

		public static ViewExpression SampleView()
		{
			ViewExpression view = View()
				.Query(
				Where(
					FieldRef("Title", "Text").Contains("New")
						.And(FieldRef("Id", "Counter").Geq(0))
						.Or(FieldRef("Author", "User").Eq(new XElement("UserID"))
						)
					),
				OrderBy(
					FieldRef("Title", "Text").Descending(),
					FieldRef("Id", "Counter")))
				.ViewFields(FieldRef("Id", "Counter"))
				.Webs(WebsScope.Recursive);
			return view;
		}
	}
}